<template>
	<view class="container">
		<view class="top">
			<image class="top-image" src="../../static/login/bj@2x.png" mode=""></image>
			<view class="hello-text">
				您好， 
			</view>
			<view class="welcome-text">
				欢迎使用
			</view>
		</view>

		<view class="content">
			<view class="account">
				<u-icon name="account" color="#3E4044"></u-icon>
				<text class="account-text">账户</text>
				

			</view>
			
			
			<view class="account-write">
				<input placeholder-class="account-placeholder"  type="text" v-model="accunt" placeholder="输入您的账号" />
			</view>
			<view class="account" >
				<u-icon name="lock" color="#3E4044"></u-icon>
				<text class="account-text">密码</text>
			</view>
			<view class="account-write">
				<u-input style="flex: 1;align-items: center;" placeholder-class="account-placeholder"  type="text" v-model="password" placeholder="输入您的密码" :password-icon="passwordIcon"
					:clearable='false' />
			</view>
			<view class="login-btn" @tap="getWxcode">
				登录
			</view>
			<!-- <view class="company-name">
				©2018-2023 hengzhiyi.cn 
			</view>
			<view class="company-name" style="margin-top: 0rpx;">
				版权所有 粤ICP备16043570号
			</view> -->
		</view>
		<!-- 登录成功提示 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		auth,login,verifyToken
	} from "@/api/login.js"
	export default {
		data() {
			return {
				accunt: '',
				password: '',
				type: 'password',
				passwordIcon: true,
				memberId:0
			};
		},
		onLoad() {
			if(uni.getStorageSync('token')){
				this.verifyTokenHandle()
			}
			
		},
		methods: {
			intervieweeLogin(){
				uni.navigateTo({
					url:'/pages/index/inter_index/inter_index'
				})
			},
			// 验证token
			async verifyTokenHandle(){
				
				if(!uni.getStorageSync('token')){
					this.loginHandler()
					return
				}
				const res =await verifyToken({
					token:uni.getStorageSync('token')
				})
				if(res.data.tokenValid){
					uni.switchTab({
						url:'/pages/index/index'
					})
				}else{
					this.loginHandler()
				}
			},
			// 登录
			async loginHandler(){
				const res =await login({
					memberId:this.memberId,
					username:this.accunt,
					password:this.password
				})
				console.log(res)
				if(res.code==0){
					uni.setStorageSync('token',res.data.token)
					uni.navigateTo({
						url:'/pages/hotelsList/hotelsList'
					})
				}
			},
			// 点击获取微信
			async getWxcode(){
				if(!this.accunt||!this.password){
					uni.showToast({
						title:'请填入账号和密码',
						icon:'none'
					})
					return
				}
				let that=this
				wx.login({
				    success: async (data) => {
				      // 注意这里我们使用了 async 函数，以便在内部使用 await
				      try {
				        const res = await auth({
				          appConfigId: 1,
				          code: data.code
				        });
				        console.log(res);
						if(res.code==0){
							
								that.memberId=res.data.memberId
								that.verifyTokenHandle()
							
						}
				      } catch (error) {
				        // 处理错误
				        console.error('登录认证失败', error);
				      }
				    },
				    fail: (error) => {
				      // 处理登录失败
				      console.error('微信登录失败', error);
				    }
				  });
				// uni.navigateTo({
				// 	url:'/pages/hotelsList/hotelsList'
				// })
				return
				
				const res = await loginRequest({
					userName: this.accunt,
					password: this.password,
					systemId :2
				})
				console.log(res)
				if (res.result.resultCode == 2002) {
					uni.showToast({
						title:'用户名或密码错误',
						icon:'none'
					})
					
				} else if (res.result.resultCode == 2003) {
					uni.showToast({
						title:'非工作时间，不允许登录',
						icon:'none'
					})
					
				}
				else if(res.result.resultCode==0){
					uni.setStorageSync('menus',res.result.menus||[])
					uni.setStorageSync('user',res.result.user)
					uni.setStorageSync('token',res.result.token)
					uni.showToast({
						title:"登录成功",
						icon:'none'
					})
					// let arrry = Object.values(res.menus)[0]
					// let right = arrry.some(item => item.name === '面试者答题');
					// if(right){
					// 	uni.reLaunch({
					// 		url:'/pages/index/inter_index/inter_index'
					// 	})
					// }else{
						
						
					// }
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/index/index'
						})
					},1500)
					
				}
				else{
					uni.showToast({
						title:'登录出错!请联系管理员',
						icon:'none'
					})
				}
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
	}

	.top {
		
		width: 750rpx;
		height: 480rpx;
		
		overflow: hidden;
		position: relative;
	}
	.top-image{
		position: absolute;
		left: 0;
		top: 0;
		width: 750rpx;
		height: 480rpx;
		z-index: -1;
	}

	.hello-text {
		font-size: 44rpx;
		font-family: PingFang SC;
		font-weight: bold;
		opacity: 1;
		color: #FFFFFF;
		margin-left: 32rpx;
		margin-top: 200rpx;
	}

	.welcome-text {
		font-size: 44rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-weight: 500;

		color: #FFFFFF;
		margin-top: 14rpx;
		margin-left: 32rpx;
	}

	.content {
		position: absolute;
		left: 0;
		top: 440rpx;
		width: 750rpx;
		height: calc(100vh - 440rpx);

		background: #FFFFFF;
		border-radius: 40rpx 40rpx 0rpx 0rpx;
		padding: 100rpx 60rpx;
		box-sizing: border-box;
	}

	.account {
		display: flex;
		align-items: center;
	}
	.account:nth-child(n+1){
		margin-top: 20px;
	}

	.account-text {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		margin-left: 16rpx;
		color: #3E4044;
	}

	.account-write {
		display: flex;
		align-items: center;
		flex: 1;
		height: 100rpx;
		border-bottom: 1px solid #E7E8EF;
		/deep/ .uni-input-input{
			font-size: 14px !important;
		}
	}
	
	.account-placeholder{
		font-size: 14px !important;
	}

	.u-input {
		align-items: center;
		padding-right: 20rpx !important;
		box-sizing: border-box !important;
		
		.uni-input-placeholder{
			font-size: 14px !important;
		}
		/deep/ .u-icon__icon{
			font-size: 14px !important;
		}
	}
	

	.login-btn {
		margin-top: 64rpx;
		width: 626rpx;
		height: 82rpx;
		background: linear-gradient(90deg, #4448EB 0%, #4B63FF 100%);
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 82rpx;
		text-align: center;
		color: #FFFFFF;
		border-radius: 28rpx;
	}

	.company-name {

		margin-top: 300rpx;
		display: flex;
		justify-content: center;
		font-size: 24rpx;
		font-family: Source Han Sans SC;
		font-weight: 400;
		color: #8C8F9A;

	}
	
	// ipad样式
	
</style>
